package com.hanjunlan.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hanjunlan.mall.entity.WnManager;
import com.hanjunlan.mall.dao.WnManagerMapper;
import com.hanjunlan.mall.exception.AccountExistException;
import com.hanjunlan.mall.service.IWnManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanjunlan.mall.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gcbsh
 * @since 2025-04-08
 */
@Service
public class WnManagerServiceImpl extends ServiceImpl<WnManagerMapper, WnManager> implements IWnManagerService {
   @Autowired
   private WnManagerMapper managerMapper;

    /**
     * 实现添加管理员功能 账号重复时抛出异常
     * @param manager
     */
    @Override
    public ResponseData addManager(WnManager manager) {
        //判断管理员账号是否已经存在
        QueryWrapper<WnManager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",manager.getAccount());
        Long count = managerMapper.selectCount(queryWrapper);
        //如果存在
        if(count>0){
            throw  new AccountExistException("账号"+manager.getAccount()+"已经存在");
        }
        //如果不存在，直接增加到表种
        managerMapper.insert(manager);
        return ResponseData.sucecss("注册成功！");
    }

    /***
     * 实现管理员登录功能
     * @param account 账号
     * @param password 密码
     * @return 当前登录管理员用户信息
     */
    @Override
    public String login(String account, String password) {
        LambdaQueryWrapper<WnManager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WnManager::getAccount,account);
        queryWrapper.eq(WnManager::getPassword,password);
        WnManager manager = managerMapper.selectOne(queryWrapper);
        if(manager==null){
            return "error";
        }
        manager.setStatus("alreadyLogin");
        return "home";
    }
}
